// pages/wallet/tixian.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js");
import { hexMD5 } from '../../../utils/md5.js';
import Toast from '../../../component/vant/toast/toast';
import Dialog from '../../../component/vant/dialog/dialog';
const CryptoJS = require("../../../utils/aesUtil.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        payMoney: 0,
        submitState: false,
        showDialog: false,
        payPwd: '',
        is_has_pay_pwd: 0,
        totalAmount: 0,
        fee: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onShow: function (options) {
        this.onHttpInit();
    },
    /**
     * 获取页面数据的 http请求
     */
    onHttpInit: function () {
        var that = this
        fetchApi.httpPost('/wxUser/getAccountInfo', {}, function (res) {
            console.log("账号绑定 数据", res)
            if (res.data.code == 0) {
                that.data.is_has_pay_pwd = res.data.data.is_has_pay_pwd;
                that.setData({
                    totalAmount: res.data.data.totalAmount,
                })
            }
        }, function () {})
    },
    payPwdInput(e) {
        var ps = hexMD5(e.detail);
        console.log("加密后密码：", ps);
        this.data.payPwd = ps;
    },
    inputMoney(e) {
        console.log('inputMoney', e);
        // var money = e.detail.value;
        // if (money == "") {
        //     money = "0";
        // }
        let value = e.detail.value
        if (value != '') {
            value = value.replace(/[^\d.]/g, "")
            if (/^(\d?)+(\.\d{0,2})?$/.test(value)) {} else {
                value = value.substring(0, value.length - 1);
            }
            value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
            if (value.indexOf(".") < 0 && value != "") {
                value = parseFloat(value);
            }
            // if (Number(value) < 1) {
            //     value = 1
            // }
        }else{
            value = "0";
        }

        this.data.payMoney = value;
        // var mo = this.data.payMoney * 6 / 1000;
        // var fee = mo.toFixed(2);

        this.setData({
            payMoney: value,
            // fee: fee,
        })
    },
    submitCash() {
        if (this.data.payMoney <= 0) {
            Toast.fail("充值金额必须大于0");

            this.setData({
                submitState: false,
            })
            this.data.submitState = false;
            return;
        }
        this.onHttpPayCash();
    },
    /**
     * 获取页面数据的 http请求
     */
    onHttpPayCash: function () {
        console.log('onHttpPayCash')
        this.setData({
            submitState: true,
        })
        this.data.submitState = true;
        var that = this
        fetchApi.httpPost('/self/vip/recharge', {
            pay_money: that.data.payMoney,
        }, function (res) {
            console.log("充值响应", res)
            if (res.data.code == 0) {
                // Toast.success(res.data.msg)
                var wxpayinfo = JSON.parse(res.data.data);
                if (wxpayinfo.result_code == 'SUCCESS') {
                    wx.requestPayment({
                        timeStamp: wxpayinfo.timeStamp,
                        nonceStr: wxpayinfo.nonceStr,
                        package: 'prepay_id=' + wxpayinfo.prepay_id,
                        signType: wxpayinfo.signType,
                        paySign: wxpayinfo.paySign,
                        success(res) {
                            console.log('requestPayment success', res)
                            setTimeout(function () {
                                wx.navigateBack({
                                    delta: -1,
                                })
                            }, 500)
                        },
                        fail(res) {
                            console.log('requestPayment fail', res)
                        }
                    })
                }
            }else{
                Toast.fail(res.data.msg);
            }

            setTimeout(function () {
                that.setData({
                    submitState: false,
                })
                that.data.submitState = false;
            }, 1000)
        }, function () {
            that.setData({
                netWork: false,
                hasData: false
            })
        })
    },
})